package com.luck.easyfoodbatch.ui;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.LatestInformationAdapter;
import com.luck.easyfoodbatch.mvp.home.HomeFindModel;
import com.luck.easyfoodbatch.mvp.home.HomeFindPresenter;
import com.luck.easyfoodbatch.mvp.home.HomeFindView;
import com.luck.easyfoodbatch.mvp.model.FindNewsModel;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.EasyRecyclerView;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LatestInformationActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, RecyclerArrayAdapter.OnLoadMoreListener, HomeFindView {

    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    private LatestInformationAdapter adapter;
    private List<FindNewsModel.DataBean.ModelsBean> list = new ArrayList<>();
    private HomeFindModel homeFindModel;
    private int type = 0;
    private int totalPage = 1;
    private int page = 1;
    private int isRefresh = 0;// 0普通请求，1下拉刷新，2上拉加载

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_latest_information, "最新资讯", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", 0);
        showProgressDialog(getString(R.string.app_loading));
        initViews();
    }

    /**
     * 初始化
     */
    private void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5F), getResources().getColor(R.color.line_color)));
        recyclerView.setLayoutManager(manager);
        adapter = new LatestInformationAdapter(mContext);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                FindNewsModel.DataBean.ModelsBean bean = list.get(position);
                Bundle bundle = new Bundle();
                bundle.putString("fid", bean.getFid() + "");
                startAct(InformationDetailsActivity.class, bundle);
            }
        });
        homeFindModel = new HomeFindPresenter(this, mContext);
        homeFindModel.getHomeFindNewsBanner(type + "");
        adapter.addAll(list);
        recyclerView.setAdapter(adapter);
        recyclerView.setRefreshListener(this);
    }

    @Override
    public void onRefresh() {
        isRefresh = 0;
        page = 1;
        homeFindModel.getHomeFindNewsBanner(type + "");
    }

    @Override
    public void onLoadMore() {
        //刷新
        isRefresh = 1;
        page++;
        if (!isNetworkConnected()) {
            adapter.pauseMore();
            return;
        }
        if (page > totalPage) {
            adapter.stopMore();
            return;
        }
        homeFindModel.getHomeFindNewsBanner(type + "");
    }

    @Override
    public void getBannerNewsSuccess(FindNewsModel model) {
        if (model.getStatus() == HttpUtil.RESULT_OK) {
            totalPage = model.getData().getTotalPage();
            // 下拉刷新 清空集合
            switch (isRefresh) {
                case 0:
                    list.clear();
                    list = model.getData().getModels();
                    break;
                case 1:
                    // 下拉刷新
                    list.addAll(model.getData().getModels());
                    break;
            }
            adapter.clear();
            adapter.addAll(list);
            adapter.notifyDataSetChanged();
        } else {
            T.showToast(mContext, model.getMsg());
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }
}
